package com.hro.rickpimines.project4.vogelspot1;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class VogelInfoActivity extends Activity
{
	private Spinner spinner;
	private WebView mWebView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vogelinfoactivity);
		
		spinner = (Spinner) findViewById(R.id.spinner);
		mWebView = (WebView) findViewById(R.id.webview);
		mWebView.setInitialScale(1);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setBuiltInZoomControls(true);
		
		populateSpinner();

		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			
			public void onItemSelected(AdapterView<?> parentView,
					View selectedItemView, int position, long id)
			{
				String vogel = (String) spinner.getItemAtPosition(position);

				if (!vogel.toLowerCase().contains(" ")) {
					mWebView.loadUrl("http://vogelvisie.nl/soort/"
							+ vogel.toLowerCase() + ".php");
				}

			}

			
			public void onNothingSelected(AdapterView<?> parentView)
			{
				// your code here
			}

		});
	}

	private void populateSpinner()
	{
		Vogels v = new Vogels();
		ArrayList<String> list = new ArrayList<String>();
		list.add("Geen selectie");

		for (String vogelNaam : v.getVogelNamen()) {
			list.add(vogelNaam);
		}

		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		dataAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(dataAdapter);
	}
}
